import request from "../request.js"
import store from '@/store/index.js'
import { TOKEN } from '@/constant/index.js'

const base_url = 'https://api.wxls.pro/cms'

function getToken(){
	const header = { "content-type": "application/json" }
	const yToken = store.getters.yToken || uni.getStorageSync(TOKEN)
	if(yToken){
		header['Authorization'] = `bearer ${yToken}`
	}
	return header
}

/**
 * 阅读文章
 */
export const read = async (par,wxlsToken) => {
	const header = getToken()
	let { data } = await request({
		url: `${base_url}/article-read/read`,
		header,
		method: 'POST',
		data:par
	});
	if (!data.success) {
		uni.showToast({
			title: data.message || '获取失败',
			duration: 2000,
			icon: 'none'
		});
		return Promise.reject(new Error(data.message || '获取失败'))
	}
	return data.data
}

/**
 * 获取指定时间的阅读详情
 */
export const list = async (par,wxlsToken) => {
	const header = getToken()
	let { data } = await request({
		url: `${base_url}/article-read/list-articles/${par.buzId}`,
		header,
		method: 'GET',
		data:par
	});
	if (!data.success) {
		uni.showToast({
			title: data.message || '获取失败',
			duration: 2000,
			icon: 'none'
		});
		return Promise.reject(new Error(data.message || '获取失败'))
	}
	return data.data
}